package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.Goods;
import com.ruoyi.mall.domain.Order;
import com.ruoyi.mall.mapper.OrderBanchMapper;
import com.ruoyi.mall.mapper.OrderMapper;
import com.ruoyi.mall.service.IOrderService;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单列Controller
 *
 * @author ruoyi
 * @date 2025-04-28
 */
@RestController
@RequestMapping("/shangpin/order")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IUserService userInfoService;
    @Resource
    private OrderBanchMapper orderBanchMapper;

    /**
     * 查询订单列列表
     */
    @PreAuthorize("@ss.hasPermi('mall:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        if (StringUtils.isEmpty(order.getGoodIdName()) == false) {
            Long id = orderMapper.selectId(order.getGoodIdName());
            if (id == null) {
                return null;
            }
            order.setGoodId(id);
        }
        if (StringUtils.isEmpty(order.getUserPhone()) == false) {
            Integer id = orderMapper.selectIdPhone(order.getUserPhone());
            if (id == null) {
                return null;
            }
            order.setUserId(id.longValue());
        }
        startPage();
        order.setIsDeleted(0);
        List<Order> list = orderService.selectOrderList(order);
        for (Order order1 : list) {
            //查询背包发货订单商品列表
            User users = userInfoService.selectUserById(order1.getUserId());
            if (!ObjectUtils.isEmpty(users)){
                order1.setUserPhone(users.getPhone());
                order1.setUserName(users.getName());
            }
            if (order1.getIsBatch() == 0) {
                Goods goods = orderMapper.selectOrder(order1.getGoodId().intValue());
                order1.setGoodIdImg(goods.getImgShow());
                order1.setGoodIdName(goods.getName());
            } else {

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出订单列列表
     */
    @PreAuthorize("@ss.hasPermi('mall:order:export')")
    @Log(title = "订单列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order) {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单列数据");
    }

    /**
     * 获取订单列详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        Order order = orderService.selectOrderById(id.longValue());
        User users = userInfoService.selectUserById(order.getUserId());
        order.setUserPhone(users.getPhone());
        order.setUserName(users.getName());
        if (order.getIsBatch() == 0) {
            Goods goods = orderMapper.selectOrder(order.getGoodId().intValue());
            order.setGoodIdImg(goods.getImgShow());
            order.setGoodIdName(goods.getName());
        } else {

        }
        return success(order);
    }

    /**
     * 新增订单列
     */
    @PreAuthorize("@ss.hasPermi('mall:order:add')")
    @Log(title = "订单列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单列
     */
    @PreAuthorize("@ss.hasPermi('mall:order:edit')")
    @Log(title = "订单列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
//        Order old = orderService.selectOrderById(order.getId());
//        if (order.getTkState().equals(2) && old.getTkState().equals(0)) {
//            //退款失败
//            order.setOrderState(old.getTkqzt());
//        }
        if (order.getOrderState() == 3) {
            order.setFahuoTime(new Date());
            order.setExpress(order.getExpress());
            order.setCourierNumber(order.getCourierNumber());
        }
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单列
     */
    @PreAuthorize("@ss.hasPermi('mall:order:remove')")
    @Log(title = "订单列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        for (Integer id : ids) {
            Order object = new Order();
            object.setId(id.longValue());
            object.setIsDeleted(1);
            orderService.updateOrder(object);
        }
        return toAjax(1);
    }
}
